<?php
namespace app\common\validate\member;
use app\common\model\member\MemberAccountModel;
use think\Validate;
/**
 * 接单账号验证器
 */
class MemberAccountValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'member_id' => 'require',
        'store_platform_id' => 'require',
        'nickname' => 'require',
        'accname' => ['require', 'checkAccnameExisted'],
        'is_disable' => ['require', 'in' => '0,1'],
        'status' => ['require', 'in' => '0,1,2'],
        'serial_num' => ['require', 'in' => '0,1,2,3,4,5'],
        'sex' => ['require', 'in' => '1,2'],
        'age_index' => ['require'],
        'taoling' => ['require'],
        'level' => ['require'],
        'integral' => ['require'],
        'evaluate_image_id' => 'require',
        'last_month_image_id' => 'require',
        'month_image_id' => 'require',
        'consignee' => ['require'],
        'phone' => ['require', 'mobile', 'checkPhoneExisted'],
        'region_id' => ['require'],
        'address' => ['require', 'checkAddressExisted'],
        'account_list'=> ['require'],
    ];
    // 错误信息
    protected $message = [
        'member_id.require'=> '请选择买手',
        'store_platform_id.require'=> '请选择店铺平台',
        'nickname.require'=> '请输入接单昵称',
        'accname.require'=> '请输入接单账号',
        'status.require' => '请选择审核状态',
        'serial_num.require' => '请回退重新选择平台',
        'sex.require' => '请选择性别',
        'age_index.require' => '请选择年龄',
        'taoling.require' => '请输入淘龄',
        'level.require' => '请选择信用等级',
        'integral.require' => '请输入淘气值',
        'evaluate_image_id.require' => '请上传订单评价截图',
        'last_month_image_id.require' => '请上传上月订单截图',
        'month_image_id.require' => '请上传上上月订单截图',
        'consignee.require' => '请输入收货人姓名',
        'phone.require' => '请输入收货人手机号',
        'phone.mobile' => '请输入正确的手机号码',
        'region_id.require' => '请选择收货地址',
        'address.require' => '请输入详细地址',
        'account_list.require' => '请选择开启接单的账号',
    ];
    // 验证场景
    protected $scene = [
        'info'    =>  ['id'],
        'add'     =>  ['member_id', 'store_platform_id',  'accname'],
        'edit'    =>  ['id','member_id', 'store_platform_id',  'accname'],
        'dele'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
        'auth'    =>  ['ids', 'status'],
        'save'    =>  ['store_platform_id', 'serial_num',  'accname','sex','age_index','setting_store_type_ids','consignee','phone','region_id','address'],
        'setOrderTaking'=>['member_id','account_list']
    ];

    /**
     * 验证账号
     * @Author: 易军辉
     * @DateTime:2024-08-06 20:28
     * @param $value
     * @param $rule
     * @param $data
     * @return string|true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function checkAccnameExisted($value, $rule, $data = [])
    {
        $model = new MemberAccountModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['accname', '=', $data['accname']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '账号已存在:' . $data['accname'];
        }
        return true;
    }

    /**
     * 验证收货人姓名
     * @Author: 易军辉
     * @DateTime:2024-08-06 20:28
     * @param $value
     * @param $rule
     * @param $data
     * @return string|true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function checkConsigneeExisted($value, $rule, $data = [])
    {
        $model = new MemberAccountModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['member_id', '=', $data['member_id']];
        $where[] = ['store_platform_id', '=', $data['store_platform_id']];
        $where[] = ['consignee', '=', $data['consignee']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '收货人姓名已存在：' . $data['consignee'];
        }
        return true;
    }
    /**
     * 验证收货人手机号
     * @Author: 易军辉
     * @DateTime:2024-08-06 20:28
     * @param $value
     * @param $rule
     * @param $data
     * @return string|true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function checkPhoneExisted($value, $rule, $data = [])
    {
        $model = new MemberAccountModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['member_id', '=', $data['member_id']];
        $where[] = ['store_platform_id', '=', $data['store_platform_id']];
        $where[] = ['phone', '=', $data['phone']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '收货人手机号已存在：' . $data['phone'];
        }
        return true;
    }
    /**
     * 验证详细地址
     * @Author: 易军辉
     * @DateTime:2024-08-06 20:28
     * @param $value
     * @param $rule
     * @param $data
     * @return string|true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function checkAddressExisted($value, $rule, $data = [])
    {
        $model = new MemberAccountModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['member_id', '=', $data['member_id']];
        $where[] = ['store_platform_id', '=', $data['store_platform_id']];
        $where[] = ['region_id', '=', $data['region_id']];
        $where[] = ['address', '=', $data['address']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '详细地址已存在：' . $data['address'];
        }
        return true;
    }




}
